﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using CommunitySystemModel.WordMode;
using System.Data;
using System.IO;

namespace CommunitySystem.BO.WordExport
{
    public class PositionChangeBO
    {
        public ObservableCollection<Word_PostionChange> GetWord_PostionChange(string condition)
        {
            return AccessHelper.GetInstance().GetEnity<Word_PostionChange>(condition, "");
        }

        public string SaveWord_PostionChange(Word_PostionChange enity)
        {
            AccessHelper.GetInstance().SaveEntity(enity, "", null);
            
            return "";
            //if (string.IsNullOrEmpty(enity.NewDepartment))
            //{               
            //    return Export(header, enity.Name, enity.OrginalDepartment, enity.OrginalPostion, enity.Reason, enity.AssginDepartment, enity.SignPerson, (DateTime)enity.CreateDate);
            //}
            //else
            //{
            //    return Export(header, enity.Name, enity.OrginalDepartment, enity.OrginalPostion, enity.Reason,enity.NewDepartment, enity.AssginDepartment, enity.SignPerson, (DateTime)enity.CreateDate);
            //}
        }

        public string ExportWordPositionChange(int id)
        {
            var enity = this.GetWord_PostionChange("id=" + id).FirstOrDefault();
            string header = string.Format("No.:{0}[{1}]                                     No.:{0}[{1}]", enity.Year, enity.NO);
            if (string.IsNullOrEmpty(enity.NewDepartment))
            {
                return Export(header, enity.Name, enity.OrginalDepartment, enity.OrginalPostion, enity.Reason, enity.AssginDepartment, enity.SignPerson, (DateTime)enity.CreateDate);
            }
            else
            {
                return Export(header, enity.Name, enity.OrginalDepartment, enity.OrginalPostion, enity.Reason, enity.NewDepartment, enity.AssginDepartment, enity.SignPerson, (DateTime)enity.CreateDate);
            }
            
        }

        public void DeleteWord_PostionChange(int id)
        {
            AccessHelper.GetInstance().DeleteEntity("Word_PostionChange", id);
        }

        /// <summary>
        /// 获取制定年份最高文号
        /// </summary>
        /// <param name="Year"></param>
        /// <returns></returns>
        public int GetNOByYear(int Year)
        {
            string sql  = "select top 1 [NO] from Word_PostionChange where [Year]="+ Year +" order by [NO] desc";
            DataSet ds = AccessHelper.GetInstance().ExcuteDataSet(sql);
            if (ds.Tables[0].Rows.Count > 0)
                return (int)ds.Tables[0].Rows[0][0];
            return 0;
        }

        /// <summary>
        /// 有新单位
        /// </summary>
        /// <param name="header"></param>
        /// <param name="name"></param>
        /// <param name="OrginalDepart"></param>
        /// <param name="OrginalPostion"></param>
        /// <param name="reason"></param>
        /// <param name="DestDepart"></param>
        /// <param name="AssignDepart"></param>
        /// <param name="Operator"></param>
        /// <param name="createDate"></param>
        /// <returns></returns>
        public string Export(string header, string name, string OrginalDepart, string OrginalPostion, string reason, string DestDepart, string AssignDepart, string Operator, DateTime createDate)
        {
            WordExportBO utility = new WordExportBO();
            WordExportBO.KillAllWordProcess();
            var _word = new Microsoft.Office.Interop.Word.Application();
            _word.Visible = true;//设置Word应用程序为不可见
            var miss = Type.Missing;
            //string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/wordTemplete");
           // object path = @"D:\PostionChanges.doc";// @"行政介绍信模版.doc";           
            string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/wordTemplete");
            object path = uploadFolder + @"\" + @"PostionChanges.doc";// @"行政介绍信模版.doc";
            var doc = _word.Documents.Open(ref path, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
                ref miss, ref miss);
            utility.SetPageHeader(_word, header);


            utility.MoveLine(_word, 5);
            utility.MoveRight(_word, 3);

            int count = name.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(name);

            utility.MoveRight(_word, 13 - count * 2);
            count = OrginalDepart.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(OrginalDepart);

            utility.MoveRight(_word, 33 - count * 2);
            count = OrginalPostion.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(OrginalPostion);

            utility.MoveRight(_word, 32 - count * 2);
            count = reason.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(reason);

            utility.MoveRight(_word, 34 - count * 2);
            count = DestDepart.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(DestDepart);

            utility.MoveLine(_word, 5);
            _word.Selection.InsertAfter(AssignDepart);

            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(Operator);

            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(string.Format("{0}年 {1}月 {2}日", createDate.Year, createDate.Month, createDate.Day));


            utility.MoveRight(_word, 1);
            utility.MoveLine(_word, 5);

            utility.MoveRight(_word, 3);
            utility.MoveLine(_word, 4);
            count = name.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(name);

            utility.MoveRight(_word, 13 - count * 2);
            count = OrginalDepart.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(OrginalDepart);

            utility.MoveRight(_word, 33 - count * 2);
            count = OrginalPostion.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(OrginalPostion);

            utility.MoveRight(_word, 32 - count * 2);
            count = reason.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(reason);

            utility.MoveRight(_word, 34 - count * 2);
            count = DestDepart.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(DestDepart);

            utility.MoveLine(_word, 5);
            _word.Selection.InsertAfter(AssignDepart);

            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(Operator);

            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(string.Format("{0}年 {1}月 {2}日", createDate.Year, createDate.Month, createDate.Day));
            string saveFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/cash");
            object savepath = saveFolder + @"\" + @"岗位变更通知单.doc";// @"行政介绍信模版.doc";
            if (File.Exists(savepath.ToString()))
                File.Delete(savepath.ToString());
            doc.SaveAs(ref savepath, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);

            doc.Close(miss, miss, miss);

            _word.Quit();
            WordExportBO.KillAllWordProcess();
            return "/cash/岗位变更通知单.doc";
        }


        /// <summary>
        /// 没有新单位
        /// </summary>
        /// <param name="header"></param>
        /// <param name="name"></param>
        /// <param name="OrginalDepart"></param>
        /// <param name="OrginalPostion"></param>
        /// <param name="reason"></param>
        /// <param name="AssignDepart"></param>
        /// <param name="Operator"></param>
        /// <param name="createDate"></param>
        /// <returns></returns>
        public string Export(string header, string name, string OrginalDepart, string OrginalPostion, string reason, string AssignDepart, string Operator, DateTime createDate)
        {
            WordExportBO utility = new WordExportBO();
            WordExportBO.KillAllWordProcess();
            var _word = new Microsoft.Office.Interop.Word.Application();
            _word.Visible = false;//设置Word应用程序为不可见
            var miss = Type.Missing;
            //string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/wordTemplete");
            //object path = @"D:\PostionChanges2.doc";// @"行政介绍信模版.doc";           
            string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/wordTemplete");
            object path = uploadFolder + @"\" + @"PostionChanges2.doc";// @"行政介绍信模版.doc";
            var doc = _word.Documents.Open(ref path, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
                ref miss, ref miss);
            utility.SetPageHeader(_word, header);


            utility.MoveLine(_word, 5);
            utility.MoveRight(_word, 3);

            int count = name.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(name);

            utility.MoveRight(_word, 13 - count * 2);
            count = OrginalDepart.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(OrginalDepart);

            utility.MoveRight(_word, 33 - count * 2);
            count = OrginalPostion.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(OrginalPostion);

            utility.MoveRight(_word, 32 - count * 2);
            count = reason.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(reason);

            //utility.MoveRight(_word, 34 - count * 2);
            //count = DestDepart.Length;
            //_word.Selection.Delete(Type.Missing, count * 2);
            //_word.Selection.InsertAfter(DestDepart);

            utility.MoveLine(_word, 7);
            _word.Selection.InsertAfter(AssignDepart);

            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(Operator);

            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(string.Format("{0}年 {1}月 {2}日", createDate.Year, createDate.Month, createDate.Day));


            utility.MoveRight(_word, 1);
            utility.MoveLine(_word, 5);

            utility.MoveRight(_word, 3);
            utility.MoveLine(_word, 4);
            count = name.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(name);

            utility.MoveRight(_word, 13 - count * 2);
            count = OrginalDepart.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(OrginalDepart);

            utility.MoveRight(_word, 33 - count * 2);
            count = OrginalPostion.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(OrginalPostion);

            utility.MoveRight(_word, 32 - count * 2);
            count = reason.Length;
            _word.Selection.Delete(Type.Missing, count * 2);
            _word.Selection.InsertAfter(reason);

            //utility.MoveRight(_word, 34 - count * 2);
            //count = DestDepart.Length;
            //_word.Selection.Delete(Type.Missing, count * 2);
            //_word.Selection.InsertAfter(DestDepart);

            utility.MoveLine(_word, 7);
            _word.Selection.InsertAfter(AssignDepart);

            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(Operator);

            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(string.Format("{0}年 {1}月 {2}日", createDate.Year, createDate.Month, createDate.Day));

            string saveFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/cash");
            object savepath = saveFolder + @"\" + @"岗位变更通知单.doc";// @"行政介绍信模版.doc";
            if (File.Exists(savepath.ToString()))
                File.Delete(savepath.ToString());
            doc.SaveAs(ref savepath, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);

            doc.Close(miss, miss, miss);

            _word.Quit();
            WordExportBO.KillAllWordProcess();
            return "/cash/岗位变更通知单.doc";
        }
    }
}
